<template>
  <div class="flex">
    <BasicTable class="w-1/2" @register="registerTable" @row-click="handleRowClick">
      <template #toolbar>
        <a-button
          v-auth="['system:dict:category:create']"
          type="primary"
          pre-icon="ion:add"
          @click="handleCreate"
        >
          {{ t('common.create') }}
        </a-button>
      </template>
      <!-- 状态 -->
      <template #status="{ record }">
        <Tag :color="record.status ? 'success' : 'error'">
          {{ record.status ? t('common.enabled') : t('common.disabled') }}
        </Tag>
      </template>
      <!-- 是否创建目录 -->
      <template #universal="{ record }">
        <Tag :color="record.universal == 'folder' ? 'success' : 'error'">
          {{ record.universal == 'folder' ? t('common.yes') : t('common.no') }}
        </Tag>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'ion:ios-compose-outline',
              label: t('common.edit'),
              onClick: handleEdit.bind(null, record),
              auth: 'system:dict:category:update',
            },
            {
              icon: 'ion:ios-trash-outline',
              danger: true,
              label: t('common.delete'),
              popConfirm: {
                title: t('common.confirmDelete'),
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
              auth: 'system:dict:category:delete',
            },
          ]"
        />
      </template>
    </BasicTable>
    <DictData class="w-1/2" :search-info="searchInfo" />
    <DictTypeModal @register="registerModal" @success="reload()" />
  </div>
</template>

<script setup lang="ts">
  import { reactive } from 'vue';
  import { Tag } from 'ant-design-vue';
  import DictData from './DictData.vue';
  import DictTypeModal from './DictTypeModal.vue';
  import { typeColumns, typeSearchFormSchema } from './dictType';
  import { GetResourceType, DeleteResourceType } from '/@/api/demo/dict';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import { BasicTable, TableAction, useTable } from '/@/components/Table';

  const { t } = useI18n();
  const { createMessage } = useMessage();
  const [registerModal, { openModal: openDictModal }] = useModal();
  const searchInfo = reactive<Recordable>({});

  const [registerTable, { reload }] = useTable({
    titleHelpMessage: 'DictTypeIndex',
    api: GetResourceType,
    // title: t('routes.table.dictionaryManagement.dictionaryCategoryList'),
    columns: typeColumns,
    fetchSetting: {
      listField: 'item',
    },
    formConfig: {
      labelWidth: 120,
      schemas: typeSearchFormSchema,
      autoSubmitOnEnter: true,
    },
    rowKey: 'id',
    // 分页配置
    pagination: { pageSize: 50, defaultPageSize: 50 },
    useSearchForm: true,
    showTableSetting: true,
    showIndexColumn: false,
    actionColumn: {
      width: 140,
      title: t('common.action'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
  });

  function handleRowClick(record) {
    searchInfo.TypeID = record.id;
    searchInfo.dictType = record.type;
    searchInfo.universal = record.universal;
  }

  function handleCreate() {
    openDictModal(true, { isUpdate: false });
  }

  function handleEdit(record: Recordable) {
    openDictModal(true, { record, isUpdate: true });
  }

  async function handleDelete(record: Recordable) {
    await DeleteResourceType(record.id);
    createMessage.success(t('common.delSuccessText'));
    reload();
  }
</script>

<style scoped></style>
